package com.zhl.gradledemo.service.impl;

import com.zhl.gradledemo.dao.BaseDao;
import com.zhl.gradledemo.service.BaseService;
import com.zhl.gradledemo.util.DbUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author austinChen
 * Created by austinChen on 2019/4/23 20:06.
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected static final  Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);
    private static final String ID = "id";

    protected static final RowBounds NOT_LIMIT_ROW_BOUNDS=new RowBounds(0,0);
    /**
     * 返回具体的mapper对象
     * @return 返回具体的mapper对象
     */
    protected abstract BaseDao<T> getBaseDao();
    @Override
    public int insert(T t)
    {
        int i= this.getBaseDao().insert(t);
        if(i>0)
        {
            this.afterUpdateSuccess(t);
        }
        return i;
    }

    @Override
    public int insertList(List<T> lists) {
        return this.getBaseDao().insertList(lists);
    }

    @Override
    public int update(T t){
        return this.getBaseDao().updateByPrimaryKey(t);
    }

    @Override
    public int updateByExample(T t, Object example) {
        return this.getBaseDao().updateByExample(t,example);
    }

    @Override
    public int updateByExampleSelective(T t, Object example) {
        return this.getBaseDao().updateByExampleSelective(t,example);
    }

    @Override
    public int updateByPrimaryKeySelective(T t) {
        return this.getBaseDao().updateByPrimaryKeySelective(t);
    }

    @Override
    public int delete(T t){
        return this.getBaseDao().delete(t);
    }

    @Override
    public int deleteByPrimaryKey(Object key){
        return this.getBaseDao().deleteByPrimaryKey(key);
    }

    @Override
    public int deleteByIds(String ids)
    {
        if(StringUtils.isEmpty(ids))
        {
            return 0;
        }
        DbUtil.checkDeleteNum(ids.split(",").length);
        return this.getBaseDao().deleteByIds(transIds(ids));
    }

    @Override
    public int deleteByExample(Object example) {
        return this.getBaseDao().deleteByExample(example);
    }

    @Override
    public List<T> selectByExample(Example example)
    {
        return this.getBaseDao().selectByExample(example);
    }

    @Override
    public List<T> selectByExampleAndRowBounds(Example example, RowBounds rowBounds)
    {
        return this.getBaseDao().selectByExampleAndRowBounds(example,rowBounds);
    }

    @Override
    public void afterInsertSuccess(T t)
    {

    }
    @Override
    public void afterUpdateSuccess(T t)
    {

    }

    @Override
    public void afterDeleteSuccess(T t)
    {

    }
    @Override
    public int insertOrUpdate(T t){
       return insertOrUpdate(t,false);

    }



    @Override
    public int insertOrUpdate(T t,boolean updateAll){
        try
        {
            String id=BeanUtils.getProperty(t, ID);
            if(StringUtils.isEmpty(id)) {
                int result= this.getBaseDao().insertSelective(t);
                if(result>0)
                {
                    this.afterInsertSuccess(t);
                }
                return result;
            }
            else
            {
                int result=0;
                if(updateAll)
                {
                    result= this.update(t);
                }
                else
                {
                    result= this.getBaseDao().updateByPrimaryKeySelective(t);
                }
                if(result>0)
                {
                    this.afterUpdateSuccess(t);
                }
                return result;
            }
        }
        catch (Exception ex)
        {
            int result= this.getBaseDao().insert(t);
            if(result>0)
            {
                this.afterInsertSuccess(t);
            }
            return result;
        }

    }


    @Override
    public List<T> select(T t){
        return this.getBaseDao().select(t);
    }
    @Override
    public List<T> selectByRowBounds(T t, RowBounds rowBounds){
        return this.getBaseDao().selectByRowBounds(t,rowBounds);
    }
    @Override
    public  T selectOne(T t){
        return this.getBaseDao().selectOne(t);
    }
    @Override
    public T selectByPrimaryKey(Object key){
        return this.getBaseDao().selectByPrimaryKey(key);
    }

    @Override
    public List<T> selectByIds(String ids){

        return this.getBaseDao().selectByIds(transIds(ids));
    }


    protected String transIds(String ids)
    {
        String[] idsList=ids.replace("，",",").replace(" ","").split(",");
        StringBuilder sb=new StringBuilder();
        for (String str:idsList) {
            sb.append(",'"+str+"'");
        }
        ids=sb.substring(1);
        return ids;
    }

}
